﻿using System.Reflection;
using AutoMapper;
using Sharp.DataTransferObject;
using Sharp.Domain.Model;
using Sharp.Domain.PlatformBoundedContext.MessageModule.Aggregates;
using Sharp.Domain.PlatformBoundedContext.OrgModule.Aggregates;
using Sharp.Domain.PlatformBoundedContext.PermissionModule.Aggregates;
using Sharp.Domain.PlatformBoundedContext.PersonModule.Aggregates;
using Sharp.Domain.PlatformBoundedContext.PlatformModule.Aggregates;
using Sharp.Domain.PlatformBoundedContext.UserModule.Aggregates;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Crosscutting;
using Sharp.Infrastructure.Interfaces;
using Sharp.Infrastructure.IoC;

namespace Sharp.Application.Platform
{
    public class Bootstraper : IBootstrapPlugin
    {
        public void Initialize()
        {
            Mapper.Initialize(cfg =>
            {
                #region 用户
                cfg.CreateMap<UserDTO, User>()
                    .ReverseMap();
                cfg.CreateMap<UserProfileDTO, UserProfile>()
                    .ReverseMap();
                cfg.CreateMap<UserGroupDTO, UserGroup>()
                    .ReverseMap();
                cfg.CreateMap<RoleDTO, Role>()
                    .ReverseMap();
                cfg.CreateMap<PermissionDTO, Permission>()
                    .ReverseMap();
                cfg.CreateMap<Message, MessageDTO>()
                    .ReverseMap();
                cfg.CreateMap<Notice, NoticeDTO>()
                    .ReverseMap();
                #endregion

                #region 员工模块
                cfg.CreateMap<PersonDTO, Person>().ReverseMap();
                cfg.CreateMap<EmployeeDTO, Employee>().ReverseMap();
                cfg.CreateMap<GroupDTO, Group>().ReverseMap();
                cfg.CreateMap<JobDTO, Job>().ReverseMap();
                #endregion

                #region 组织
                cfg.CreateMap<OrgDTO, Org>().ReverseMap();
                cfg.CreateMap<OrgLevelDTO, OrgLevel>().ReverseMap();
                #endregion

                #region 主数据
                var application = Assembly.LoadFrom(WebServer.MapPath(@"~\bin\Sharp.Domain.PlatformBoundedContext.dll"));
                foreach (var type in application.GetTypes())
                {
                    if (type.GetInterface("IMasterData", false) == typeof(IMasterData))
                    {
                        cfg.CreateMap(type, typeof(MasterDataDTO));
                        cfg.CreateMap(typeof(MasterDataDTO), type);
                    }
                }
                #endregion

                #region 平台相关
                cfg.CreateMap<SubSystemDTO, SubSystem>().ReverseMap();
                cfg.CreateMap<UserLogDTO, UserLog >().ReverseMap();
                #endregion
            });

            if (ServiceLocator.Instance.IsRegistered<Sharp.ServiceContracts.IUserService>())
            {
                var userService = ServiceLocator.Instance.GetService<Sharp.ServiceContracts.IUserService>();
                LoggingHandler.LoginFunc = userService.SaveLog;
            }


            this.IsAlready = true;

        }

        public bool IsAlready { get; set; }
    }
}
